require 'openssl'
require 'digest'
require 'jsonify'
module YiBuService
	def self.create_work(m_type, m_id, project_id, parent_id, kezhixing)
		yibu = YiBu.new
		yibu.m_type = m_type
		yibu.m_id = m_id
		yibu.project_id = project_id
		yibu.parent_id = parent_id
		yibu.kezhixing = kezhixing
		yibu.save
		return yibu.id
	end
	def self.ry_yb()
		ryyibus = RYYiBu.where("upload_status = 0")
		ryyibus.each do |ry|
			project_user = ProjectUser.find(ry.ry_id)
			project = Project.find(project_user.project_id)
			# if project_user.upload_project_team.nil?
			# 	project_team = ProjectTeam.new
			# 	user_detail = UserDetail.find(project_user.user_id)
			# 	if user_detail.company_id.nil?
			# 		company = Company.find(project.certification_company)
			# 	else
			# 		company = Company.find(user_detail.company_id)
			# 	end
			# 	project_team.team_name = company.company_name + "默认项目班组"
			# end
			project_team = ProjectTeam.find(project_user.upload_project_team)
			bzyibus = BZYiBu.where("bz_id = ? and  upload_status =2 ",project_user.upload_project_team)
			if bzyibus.empty?
				bzyibus = BZYiBu.where("bz_id = ? and  upload_status in (0,1) ", project_user.upload_project_team)
				if bzyibus.empty?
					### 新建班组上传
					ryyibu = BZYiBu.new
					ryyibu.bz_id  = project_team.id
					ryyibu.project_id = project_team.project_id
					ryyibu.upload_status =0 
					ryyibu.save
				else
					### 继续等待班组上传
				end
			else
				WorkManageService.ry_upload(ry.id)
			end
		end
	end
	def self.qy_yb()
		qyyibus = QYYiBu.where("upload_status = 0")
		qyyibus.each do |qy|
			WorkManageService.qy_upload(qy.id)
		end
	end
	def self.bz_yb()
		bzyibus = BZYiBu.where("upload_status = 0")
		bzyibus.each do |bz|
			project_team = ProjectTeam.find(bz.bz_id)
			project = Project.find(project_team.project_id)
			cjyibu = CJYiBu.where("cj_id = ? and  upload_status =2 ",project_team.company_id)
			if cjyibu.empty?
				cjyibus = CJYiBu.where("cj_id = ? and  upload_status in (0,1) ", project_team.company_id)
				if cjyibus.empty?
					### 新建班组上传
					cjyibu = CJYiBu.new
					cjyibu.cj_id  = project_team.company_id
					cjyibu.project_id = project.id
					cjyibu.upload_status =0 
					cjyibu.save
				else
					### 继续等待班组上传
				end
			else
				WorkManageService.bz_upload(bz.id)
			end
		end
	end
	def self.cj_yb()
		cjyibus = CJYiBu.where("upload_status = 0")
		cjyibus.each do |cj|
			project = Project.find(cj.project_id)
			
			qyyibu = QYYiBu.where("qy_id = ? and  upload_status =2 and project_id = ? ",cj.cj_id,cj.project_id)
			if qyyibu.empty?
				qyyibus = QYYiBu.where("qy_id = ? and  upload_status in (0,1) and project_id = ? ",cj.cj_id,cj.project_id)
				if qyyibus.empty?
					### 新建企业上传
					qyyibu = QYYiBu.new
					qyyibu.qy_id  = cj.cj_id
					qyyibu.project_id = cj.project_id
					qyyibu.upload_status =0 
					qyyibu.save
				else
					### 继续等待企业上传
				end
			else

				WorkManageService.cj_upload(cj.id)
			end
		end
	end

	def self.kq_yb()
		kqyibus = KQYiBu.where("upload_status = 0")
		###判断人员是否上传了###
		kqyibus.each do |kq|
			sign = Sign.find(kq.kq_id)
			project_user = ProjectUser.where("project_id = ? and user_id = ?", sign.project_id, sign.user_id).first
			ryyibus = RYYiBu.where("ry_id = ? and  upload_status =2 ", project_user.id)
			if ryyibus.empty?
				ryyibus = RYYiBu.where("ry_id = ? and  upload_status in (0,1) ", project_user.id)
				if ryyibus.empty?
					### 新建员工上传
					ryyibu = RYYiBu.new
					ryyibu.ry_id  = project_user.id
					ryyibu.project_id = sign.project_id
					ryyibu.upload_status = 0
					ryyibu.save
				else
					### 继续等待人员上传
				end
			else
				jtcyibus = JTCYiBu.where("jtc_id = ? and upload_status =2  and jtc_type = 1", project_user.id)
				if jtcyibus.empty?
					jtcyibus = JTCYiBu.where("jtc_id = ? and upload_status in (0,1) and jtc_type = 1", project_user.id)
					if jtcyibus.empty?
						jtcyibu = JTCYiBu.new
						jtcyibu.jtc_id  = project_user.id
						jtcyibu.project_id = sign.project_id
						jtcyibu.upload_status = 0
						jtcyibu.save
					else
						### 等待进出场上传完成
					end
				else
					WorkManageService.kq_upload(kq.id)
				end
				
			end
			
		end
	end
	def self.gz_yb()
		gzyibus = GZYiBu.where("upload_status = 0")
		###判断人员是否上传了###
		gzyibus.each do |gz|
			parooldetail = PayrollDetail.find(gz.gz_id)

			ryyibus = RYYiBu.where("ry_id = ? and  upload_status =2 ", parooldetail.project_user_id)
			if ryyibus.empty?
				ryyibus = RYYiBu.where("ry_id = ? and  upload_status in (0,1) ", parooldetail.project_user_id)
				if ryyibus.empty?
					### 新建员工上传
					ryyibu = RYYiBu.new
					ryyibu.ry_id  = parooldetail.project_user_id
					ryyibu.project_id = parooldetail.project_id
					ryyibu.upload_status = 0
					ryyibu.save
				else
					### 继续等待人员上传
				end
			else
				#### 判断员工是否进场 ####
				jtcyibus = JTCYiBu.where("jtc_id = ? and upload_status =2  and jtc_type = 1", parooldetail.project_user_id)
				if jtcyibus.empty?
					jtcyibus = JTCYiBu.where("jtc_id = ? and upload_status in (0,1) and jtc_type = 1", parooldetail.project_user_id)
					if jtcyibus.empty?
						jtcyibu = JTCYiBu.new
						jtcyibu.jtc_id  = parooldetail.project_user_id
						jtcyibu.project_id = ryyibus.first.project_id
						jtcyibu.upload_status = 0
						jtcyibu.save
					else
						### 等待进出场上传完成
					end
				else
					WorkManageService.gz_upload(gz.id)
				end
				
			end
			
		end
	end

	# def self.px_yb()
	# 	pxyibus = GZYiBu.where("upload_status = 0")
	# 	###判断人员是否上传了###
	# 	pxyibus.each do |gz|
	# 		payroll_detail = PayrollDetail.find(gz.gz_id)
	# 		ryyibus = RYYiBu.where("ry_id = ? and  upload_status =2 ", payroll_detail.project_user_id)
	# 		if ryyibus.empty?
	# 			ryyibus = RYYiBu.where("ry_id = ? and  upload_status in (0,1) ", payroll_detail.project_user_id)
	# 			if ryyibus.empty?
	# 				### 新建员工上传
	# 				ryyibu = RYYiBu.new
	# 				ryyibu.ry_id  = payroll_detail.project_user_id
	# 				ryyibu.project_id = payroll_detail.project_id
	# 				ryyibu.upload_status = 0
	# 				ryyibu.save
	# 			else
	# 				### 继续等待人员上传
	# 			end
	# 		else
	# 			WorkManageService.gz_upload(gz.id)
	# 		end			
	# 	end
	# end

	def self.jtc_yb()
		jtcyibus = JTCYiBu.where("upload_status = 0")
		jtcyibus.each do |jtc|
			WorkManageService.jtc_upload(jtc.id)
		end
	end
	def self.ht_yb()
		project_user = ProjectUser.where("upload_status = 2 and contract_upload_status in (0,3) and contract_code IS NOT NULL")
		project_user.each do |pu|
			WorkManageService.ht_upload(pu.id)
		end	
	end
		def self.px_yb()
		project_user = ProjectUser.where("upload_status = 2 and train_upload_status in (0,3) and train_name IS NOT NULL")
		project_user.each do |pu|
			WorkManageService.px_upload(pu.id)
		end	
	end
end